/**
 * movieclip utilities
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2008. 08. 20 
*/
package com.cmiscm.utils.movieclip
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	[Event (name="play_complete", type="com.cmiscm.utils.movieclip.events.MovieClipUtilitiesEvent")]
	public class MovieClipUtilities extends EventDispatcher
	{
		public static const PLAY_COMPLETE:String = "play_complete";
		
		private static var _targetFrameDic:Dictionary = new Dictionary(true);
		
		/**
		 * Next play 
		 * @param $mc Movieclip
		 * @param $speed Speed
		 * 
		 */		
		public static function nextPlay($mc:MovieClip, $speed:int = 1):void
		{
			if ($mc.hasEventListener(Event.ENTER_FRAME)) $mc.removeEventListener(Event.ENTER_FRAME, onEnter);			
			if (_targetFrameDic[$mc] != null) delete _targetFrameDic[$mc];
			_targetFrameDic[$mc] = new Object();
			_targetFrameDic[$mc].target = $mc.totalFrames;
			_targetFrameDic[$mc].speed = $speed;
			$mc.addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
		}
		
		/**
		 * Prev play 
		 * @param $mc Movieclip
		 * @param $speed Speed
		 * 
		 */			
		public static function prevPlay($mc:MovieClip, $speed:int = 1):void
		{
			if ($mc.hasEventListener(Event.ENTER_FRAME)) $mc.removeEventListener(Event.ENTER_FRAME, onEnter);
			if (_targetFrameDic[$mc] != null) delete _targetFrameDic[$mc];
			_targetFrameDic[$mc] = new Object();
			_targetFrameDic[$mc].target = 1;
			_targetFrameDic[$mc].speed = $speed;
			$mc.addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
		}
		
		/**
		 * Target play 
		 * @param $mc Movieclip
		 * @param $start Start frame number
		 * @param $target Target frame number
		 * 
		 */			
		public static function targetPlay($mc:MovieClip, $start:int, $target:int):void
		{
			if ($mc.hasEventListener(Event.ENTER_FRAME)) $mc.removeEventListener(Event.ENTER_FRAME, onEnter);
			if (_targetFrameDic[$mc] != null) delete _targetFrameDic[$mc];
			_targetFrameDic[$mc] = new Object();
			_targetFrameDic[$mc].target = $target;
			_targetFrameDic[$mc].speed = 1;
			$mc.gotoAndStop($start);
			$mc.addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
		}
		
		/**
		 * Check movieclip frame 
		 * @param $mc Movieclip
		 * @param $target  Target frame number
		 * 
		 */		
		public static function checkPlay($mc:MovieClip, $target:int):void
		{
			if ($mc.hasEventListener(Event.ENTER_FRAME)) $mc.removeEventListener(Event.ENTER_FRAME, onEnter);
			if (_targetFrameDic[$mc] != null) delete _targetFrameDic[$mc];
			_targetFrameDic[$mc] = new Object();
			_targetFrameDic[$mc].target = $target;
			$mc.play();
			$mc.addEventListener(Event.ENTER_FRAME, onEnterCheck, false, 0, true);
		}
		
		/**
		 * Delete movieclip play 
		 * @param $mc MovieClip
		 * 
		 */		
		public static function garbagePlay($mc:MovieClip):void
		{
			if ($mc.hasEventListener(Event.ENTER_FRAME)) $mc.removeEventListener(Event.ENTER_FRAME, onEnter);
			if (_targetFrameDic[$mc] != null) delete _targetFrameDic[$mc];
		}
		
		/**
		 * Delete movieclip frame check 
		 * @param $mc MovieClip
		 * 
		 */		
		public static function garbageCheck($mc:MovieClip):void
		{
			if ($mc.hasEventListener(Event.ENTER_FRAME)) $mc.removeEventListener(Event.ENTER_FRAME, onEnterCheck);
			if (_targetFrameDic[$mc] != null) delete _targetFrameDic[$mc];
		}
		
		
		private static function onEnter(evt:Event): void
		{
			var i:int;			
			var mc:MovieClip = MovieClip(evt.currentTarget);
			if(mc.currentFrame > _targetFrameDic[mc].target){
				if (_targetFrameDic[mc].speed > 1) for (i=0; i<_targetFrameDic[mc].speed; i++) mc.prevFrame();
				else mc.prevFrame();
			} else if (mc.currentFrame < _targetFrameDic[mc].target){
				if (_targetFrameDic[mc].speed > 1) for (i=0; i<_targetFrameDic[mc].speed; i++) mc.nextFrame();
				else mc.nextFrame();
			} else {
				if (mc.hasEventListener(Event.ENTER_FRAME)) {
					mc.removeEventListener(Event.ENTER_FRAME, onEnter);
					delete _targetFrameDic[mc].target;
					mc.dispatchEvent(new Event(MovieClipUtilities.PLAY_COMPLETE));	
				}				
			}
		}
		
		private static function onEnterCheck(evt:Event): void
		{	
			var mc:MovieClip = MovieClip(evt.currentTarget);
			if(mc.currentFrame == _targetFrameDic[mc].target){
				if (mc.hasEventListener(Event.ENTER_FRAME)) {
					mc.stop();
					mc.removeEventListener(Event.ENTER_FRAME, onEnter);
					delete _targetFrameDic[mc].target;
					mc.dispatchEvent(new Event(MovieClipUtilities.PLAY_COMPLETE));	
				}		
			}
		}
		
	}
}